package stdlib

import (
	"os"
	"fmt"
	"embed"
	"io/fs"
	"strings"
	"reflect"
	"math/big"
)

//go:embed core sys rpc ui
var __FS embed.FS
var __ModuleLoadSequence = [] string {
	"core", "sys",
	// "rpc", "ui",
}
func GetModuleFileSystem() fs.FS { return __FS }
func GetModuleLoadSequence() ([] string) { return __ModuleLoadSequence }

func GetPrimitiveReflectType(which string) (reflect.Type, bool) {
	switch which {
	case Int:
		return reflect.TypeOf(big.NewInt(0)), true
	case Float:
		return reflect.TypeOf(float64(0)), true
	case Char:
		return reflect.TypeOf(int32(0)), true
	case String:
		return reflect.TypeOf(""), true
	default:
		return nil, false
	}
}

// module id
const Mod_core = "core"
const Mod_rpc = "rpc"
// types.km
const Lambda = "Lambda"
const Pair = "Pair"
const Triple = "Triple"
const ProjRef = "ProjRef"
const CaseRef = "CaseRef"
const Bool = "Bool"
const No = "No"
const Yes = "Yes"
const ( NoIndex = iota; YesIndex )
const Maybe = "Maybe"
const None = "None"
const ( NoneIndex = iota; SomeIndex )
const Either = "Either"
const Left = "L"
const Right = "R"
const ( SuccessIndex = iota; FailureIndex )
const Ordering = "Ordering"
const Smaller = "L<R"
const Equal = "L=R"
const Bigger = "L>R"
const ( SmallerIndex = iota; EqualIndex; BiggerIndex )
const ( FileReadOnly = iota; FileReadWrite; FileReadWriteCreate; FileOverwrite; FileAppend )
// error.km
const Error = "Error"
// binary.km
const Bytes = "Bytes"
// numeric.km
const Int = "Int"
const Float = "Float"
// containers.km
const Seq = "Seq"
const List = "List"
const Heap = "Heap"
const Set = "Set"
const Map = "Map"
// rx.km
const Observable = "Observable"
const Async = "Async"
const Sync = "Sync"
const Source = "Source"
const Computed = "Computed"
const Sink = "Sink"
const Bus = "Bus"
const Reactive = "Reactive"
const ReactiveEntity = "ReactiveEntity"
const ReactiveSnapshots = "ReactiveSnapshots"
const Mutex = "Mutex"
const Mutable = "Mutable"
const Buffer = "Buffer"
const HashMap = "HashMap"
// string.km
const Char = "Char"
const String = "String"

// core
const AssetBinary = "AssetBinary"
const AssetPNG = "AssetPNG"
const AssetCSS = "AssetCSS"
const AssetTTF = "AssetTTF"
type Asset struct {
	MIME  string
	Path  string
	Data  [] byte
}
func (ptr *Asset) Key() string {
	return fmt.Sprintf("%X", reflect.ValueOf(ptr).Pointer())
}

// os
type Path ([] string)
var __PathSep = string([] rune { os.PathSeparator })
func (path Path) String() string {
	return strings.Join(path, __PathSep)
}
func (path Path) Join(segments ([] string)) Path {
	for _, seg := range segments {
		if strings.Contains(seg, __PathSep) {
			panic(fmt.Sprintf("invalid path segment %s", seg))
		}
	}
	var new_path = make(Path, (len(path) + len(segments)))
	copy(new_path, path)
	copy(new_path[len(path):], segments)
	return new_path
}
func ParsePath(str string) Path {
	var raw = strings.Split(str, __PathSep)
	var path = make([] string, 0, len(raw))
	for i, segment := range raw {
		if i != 0 && segment == "" {
			continue
		}
		path = append(path, segment)
	}
	return path
}

// rpc
const ServiceIdentifier_rpc = "ServiceIdentifier"
const Method_rpc = "Method"
const MethodMulti_rpc = "Method*"


